package com.woniu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniu.dao.UserMapper;
import com.woniu.entity.User;

import com.woniu.utils.MyPage;
import com.github.pagehelper.PageHelper;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
//import org.springframework.data.domain.Example;

@Transactional
@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;

	public UserMapper getuserMapper() {
		return userMapper;
	}

	/**
	 * @return获取所有用户
	 */
	public List<User> getUserList() {
		// TODO Auto-generated method stub
		return userMapper.selectAll();
	}

	/**
	 * @return 注册用户数
	 */
	public int getUserCount() {
		// TODO Auto-generated method stub
		return userMapper.getUserCount();
	}

	/**
	 * @return 当日注册用户数
	 */
	public int countUserRegisterToday() {
		// TODO Auto-generated method stub
		return userMapper.countUserRegisterToday();
	}

	/**
	 * @return 昨日注册用户数
	 */
	public int countUserRegisterYesterday() {
		// TODO Auto-generated method stub
		return userMapper.countUserRegisterYesterday();
	}

	/**
	 * @return 本周注册用户数
	 */
	public int countUserRegisterThisWeek() {
		// TODO Auto-generated method stub
		return userMapper.countUserRegisterThisWeek();
	}

	/**
	 * @return 上周注册用户数
	 */
	public int countUserRegisterLastWeek() {
		// TODO Auto-generated method stub
		return userMapper.countUserRegisterLastWeek();
	}

	/**
	 * @return 本月注册用户数
	 */
	public int countUserRegisterThisMonth() {
		// TODO Auto-generated method stub
		return userMapper.countUserRegisterThisMonth();
	}

	/**
	 * @return 上月注册用户数
	 */
	public int countUserRegisterLastMonth() {
		// TODO Auto-generated method stub
		return userMapper.countUserRegisterLastMonth();
	}
	
	
	//criteria查询
		//相当于已经把函数功能给你封装到dao层了，直接再service层用这些函数就可以了
		public MyPage<?> userSelectByCond(String uName, String uEmail, String uPhone, String uIdcard, int pageNum, int pageSize) {
			
			//?是占位符，不确定返回什么类型
			MyPage<?> page = null;
			
			//需要查询user
			// 创建where条件
			Example exm = new Example(User.class);
			Criteria crt = exm.createCriteria();
			if (!"".equals(uName))
				crt.andCondition("uname LIKE", "%"+uName+"%"); //模糊查询
			if (!"".equals(uEmail))
				crt.andCondition("email LIKE", "%"+uEmail+"%");
			if (!"".equals(uPhone))
				crt.andCondition("tel LIKE", "%"+uPhone+"%");
			if (!"".equals(uIdcard))
				crt.andCondition("idcard LIKE", "%"+uIdcard+"%");
			
			// 查询
			//总条数要在拦截之前算出来
			int count = userMapper.selectCountByExample(exm);
			//[jqPaginator] totalCounts or totalPages is required
			//分页插件要求必须有conuts或总页数
			if (count==0)
				count=1;
			//PageHelper.startPage(pageNum, pageSize); 就是拦截 相当于limit();
			PageHelper.startPage(pageNum, pageSize);
			//符合条件的当前页要显示的数据集合list1
			List<User> list = userMapper.selectByExample(exm);
			
			//把符合条件的对象总数目、每页显示条目、当前页要显示的数据集合封装进自定义的工具类
			page = new MyPage<User>(count, pageSize, list);
			
			return page;
		}


		//查找用户-肯定能找到
		public User selectUserById(int aId) {
			return userMapper.selectByPrimaryKey(aId);
		}


		//删除用户
		public int deleteUserById(int aId) {
			return userMapper.deleteByPrimaryKey(aId);
		}
		
		
		//修改账户信息
		public int updateUser (User user) {
			Example exm = new Example(User.class);
			Criteria crt = exm.createCriteria();
			crt.andCondition("uid=",user.getUid()); //精确查询
			return userMapper.updateByExample(user, exm);
		}

		//查找用户，用于用户名形式唯一性验证
		public String selectUserByName(String uName) {
			List<User> list = null;
			Example exm = new Example(User.class);
			Criteria crt = exm.createCriteria();
			crt.andCondition("uname=",uName); //精确查询
			
			list = userMapper.selectByExample(exm);
			
			if(list.isEmpty())
				return "false";
			else
				return "true";
		}

		//新建用户
		public int createUser(User user) {
			return userMapper.insert(user);	
		}

	

}